<?php

namespace common\utils;
use yii\base\Security;

/**
 *
 * Created by PhpStorm.
 * User: WANGWEIHUA
 * Date: 2019/4/24
 * Time: 22:28
 */
class SecurityUtil
{
    //Secret 16
    const SECRET_LEN = 16;
    
    /**
     * @Function 加密数据
     * @Author Weihuaadmin@163.com
     * @param string $data 需要加密的数据 
     * @param string $secret  密码因子
     * @param string $info
     * @return string
     */
    public static function encryptByData($data, $secret, $info = null)
    {
        $security = new Security();
        return base64_encode($security->encryptByKey($data, $secret, $info));
    }

    /**
     * @Function 解密数据
     * @Author Weihuaadmin@163.com
     * @param string $data
     * @param string $password
     * @return bool|string
     */
    public static function decryptByData($data, $secret, $info = null)
    {
        $security = new Security();
        return $security->decryptByKey(base64_decode($data), $secret, $info); // TODO: Change the autogenerated stub
    }

    /**
     * @Function 生成秘钥
     * @Author Weihuaadmin@163.com
     */
    public static function generateSecret(){
        $security = new Security();
        return $security->generateRandomString(self::SECRET_LEN);
    }
}